﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmptyFolderDealWth
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckClass.DealWith();
        }

        public class CheckClass
        {
            static List<string> lstObjPath = new List<string>();
          static  List<DirectoryInfo> lstDi = new List<DirectoryInfo>();
            static public void DealWith()
            {
                string path = System.Environment.CurrentDirectory;
                Console.WriteLine("Path :" + path);
                DealWithDi(new DirectoryInfo(path));
                Console.WriteLine("Deal with empty folder !");
                DealWithEmptyDi();
                Console.WriteLine("delete \"obj\" and \"bin\" folder ？ Y / N ");
                if (Console.ReadKey().Key == ConsoleKey.Y)
                {
                    DealWithObjDi();
                }
                Console.WriteLine("completed !");
                Console.ReadKey();
            }


            static private void DealWithDi(DirectoryInfo objDi)
            {
                DirectoryInfo[] subDi = objDi.GetDirectories();
                foreach (var item in subDi)
                {
                    if ((item.Name == "obj" || item.Name == "bin"))
                    {
                        lstObjPath.Add(item.FullName);
                       // continue;
                    }

                    lstDi.Add(item);
                    DealWithDi(item);
                }
            }
            static private void DealWithEmptyDi()
            {
                foreach (var item in lstDi)
                {
                    if (Directory.GetDirectories(item.FullName).Length == 0 && Directory.GetFiles(item.FullName).Length == 0)
                    {
                        FileStream fs1 = new FileStream(item.FullName + "/ignore.keep", FileMode.Create);
                        fs1.Close();
                    }
                }
            }

            static private void DealWithObjDi()
            {
                foreach (var item in lstObjPath)
                {
                    Directory.Delete(item, true);
                }
            }

        }
    }
}
